package org.toycode.logindemo.application.tapestry.components;

import org.apache.tapestry5.ClientElement;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.RenderSupport;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.SupportsInformalParameters;
import org.apache.tapestry5.ioc.annotations.Inject;

@SupportsInformalParameters
public class Any implements ClientElement {

    @Parameter(value = "prop:componentResources.elementName", defaultPrefix =
    "literal")
    private String _element;
    @Parameter(defaultPrefix = "literal")
    private String _id;
    @Inject
    private ComponentResources _resources;
    @Environmental
    private RenderSupport _pageRenderSupport;
    private String _clientId;

    void beginRender(MarkupWriter writer) {
        writer.element(_element);

        if (_id != null) {
            _clientId = _pageRenderSupport.allocateClientId(_id);
            writer.attributes("id", _clientId);
        }

        _resources.renderInformalParameters(writer);
    }

    void afterRender(MarkupWriter writer) {
        writer.end();
    }

    public String getClientId() {
        return _clientId;
    }
}
